package com.ruoyi.test;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.event.OperLogEvent;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.system.domain.SysOperLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@Slf4j
public class SendMessage {


    @Test
    void sendMessageTest() throws IOException {

        OperLogEvent operLogEvent = new OperLogEvent();

        operLogEvent.setOperId(1l);


        String url = "http://localhost:9998/oauth/log";
        //创建httpClient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);

        //设置请求格式
        StringEntity entity = new StringEntity(JsonUtils.toJsonString(operLogEvent), "utf-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        CloseableHttpResponse response = null;
        response = client.execute(httpPost);
        if(response.getStatusLine().getStatusCode() !=200){
            response.close();
            // 记录本地异常日志
            log.error("异常信息:{}",  operLogEvent.toString());
        }
        HttpEntity entitys = response.getEntity();
        if (entitys != null) {
            String body = EntityUtils.toString(entitys, "utf-8");
            JSONObject json = JSONObject.parseObject(body);
            R r = JsonUtils.parseObject(body, R.class);
            System.out.println(r);
            response.close();
        }else {
            response.close();
        }
    }


    @Test
    public void sendMessage() {
        String key = "app-authorization";
      //  String baseURL = "http://192.180.27.62:19627/support-api/apply";
        String strTokent = null;

        // 1.取token
            // 1.取token
//            System.out.println("==================1.get Token==============");
            // 1.1 如果缓存中没有token,去平台重新获取一个。
            Object obj = RedisUtils.getCacheObject(key);
            if(obj == null) {
//                 strTokent = getAppToken("app-authorization", baseURL);
                strTokent ="fjw";
                if(strTokent!=null && strTokent.length()>0) {
                    RedisUtils.setCacheObject(key, strTokent);
                    boolean flag = RedisUtils.expire(key, Duration.ofSeconds(10));
                    System.out.println("******"+flag);
                }

                try {
                    Thread.sleep(11 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Object obj1 = RedisUtils.getCacheObject(key);
                System.out.println("11s后value的值："+obj1);
                System.out.println(strTokent.equals(obj));
            }





            if(strTokent!=null && strTokent.length()>0) {
                // 2.按平台要求发送数据
//                 uploadOperLogtoAppPlate();
                System.out.println("发送消息");
            }

    }
}
